USE [FootballPoll]
GO

/****** Object:  StoredProcedure [dbo].[GetCurrentTeamRankedByPoints]    Script Date: 12/02/2011 13:27:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetCurrentTeamRankedByPoints]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetCurrentTeamRankedByPoints]
GO

USE [FootballPoll]
GO

/****** Object:  StoredProcedure [dbo].[GetCurrentTeamRankedByPoints]    Script Date: 12/02/2011 13:27:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[GetCurrentTeamRankedByPoints] 
    /****** Pass them in as parameters  ******/
   
		@StartDate DATETIME,
		@EndDate DATETIME
     
AS 
     /****** Explicit declaration for testing  ******/
    /*
		DECLARE @StartDate DATETIME
		DECLARE @EndDate DATETIME
		SET @StartDate ='2010-04-10'
		SET @EndDate ='2010-04-16'
		*/
Select votes.teamID,  teams.teamName, SUM(votes.Points) Points, SUM(votes.FirstPlaceVotes) FP
from votes
Left Outer Join  teams
on votes.teamID = teams.id
Where
		(SELECT DATEDIFF(Day, votes.Date , @EndDate)) >= 0
and
		(SELECT DATEDIFF(Day, votes.Date , @StartDate)) <= 0
  GROUP BY teamID, teams.teamName
  
  Order by sum(votes.Points) DESC

GO

